$(document).ready(function() {
	
	$("#accordion").accordion({ 
		header: 'table',
		icons: false,
		fillSpace: true,
		active: false,
		change: function(event,ui){
			
			$("#accordion").find('.arrow_big_left').each(function(i,elem){
				$(elem).toggleClass("arrow_big_right");
				$(elem).toggleClass("arrow_big_left");
			});
			
			$(ui.newHeader).find(".arrow_big_right").toggleClass("arrow_big_left");
			$(ui.newHeader).find(".arrow_big_right").toggleClass("arrow_big_right");
		
		}
	});
	
	$.fn.raty.defaults.path = settings.base_url + 'application/assets/js/jqueryPlugins/Rating';
	$('#star').raty({
		half  : true,
		score : $("#rating").val(),
		hintList:  ['', '', '', '', ''],
		starHalf   : 'img/star-half-big.png',
		starOff    : 'img/star-off-big.png',
		starOn     : 'img/star-on-big.png',
		click : function(score, evt) {
			$.ajax({
				url: settings.base_url + lang.lang_key + '/product/add_rate',
				dataType: 'json',
				type: 'POST',
				data:{
					product_id: $("#product_id").val(),
					rating: score
				},
				success: function(data){
				},
			});
		  }
	});
	
	Shadowbox.init({displayNav:true});
	
	$("#color_search, #clarity, #metal").msDropDown();
	$("select[name='ring_size']").msDropDown();
	$("select[name='ring_size[]']").msDropDown();
	
	$("#accordion a").click(function() {
      window.location = $(this).attr('href');
      return false;
   });
	
	$("#metal").change(function(){
		$.ajax({
			url: settings.base_url + lang.lang_key + '/product/get_url_metal',
			dataType: 'json',
			type: 'POST',
			data:{
				skuRoot: $("#skuroot").val(),
				metal : $('#metal').val()
			},
			success: function(data){
				if (data.status){
					window.location = data.url;
				}
			},
		});
	});
	
	$("#color_search").change(function(){
		$.ajax({
			url: settings.base_url + lang.lang_key + '/product/get_url/' + $('#color_search').val() + '/' + $('#clarity').val(),
			dataType: 'json',
			type: 'POST',
			data:{
				current_url: $("#current_url").val()
			},
			success: function(data){
				if (data.status){
					window.location = data.url;
				}
			},
		});
	});
	
	$("#clarity").change(function(){
		$.ajax({
			url: settings.base_url + lang.lang_key + '/product/get_url/' + $('#color_search').val() + '/' + $('#clarity').val(),
			dataType: 'json',
			type: 'POST',
			data:{
				current_url: $("#current_url").val()
			},
			success: function(data){
				if (data.status){
					window.location = data.url;
				}
			},
		});
	});
	
	modal_title = '[modal_title]';
	if (lang.showing_box != undefined) {
		modal_title = lang.showing_box;
	}
	ok_text = '[ok_text]';
	if (lang.ok_text != undefined) {
		ok_text = lang.ok_text;
	}
	
	$( "#showing-box-modal" ).dialog({
		autoOpen: false,
		height: 'auto',
		width: 'auto',
		modal: true,
		buttons: [
			{
				text: ok_text,
				click: function() {
					$(this).dialog('close');
				}
			}			
		],
		title : modal_title
	});
	
	$("#showing-box").click(function(){
		$("#showing-box-modal").dialog('open');
	});
	
	$(".to_order").css("height","");
	
});

function submit_form(id, flag_fast_comand){
	if ($("#ring_size_" + id).length){
		$("#ring_size_" + id).validate();
		
		if ($("#ring_size_" + id).valid()){
			$('#fast_comand').val(flag_fast_comand);
			$("#options_" + id).submit();
		}else { 
			$("#ring_size_" + id+"_title").addClass("border_red");
		}
	} else {
		$("#ring_size_f_" + id).validate();
		$("#ring_size_m_" + id).validate();
		
		if ($("#ring_size_f_" + id).valid() && $("#ring_size_m_" + id).valid()){
			$('#fast_comand').val(flag_fast_comand);
			$("#options_" + id).submit();
		}else { 
			$("#ring_size_f_" + id+"_title").addClass("border_red");
			$("#ring_size_m_" + id+"_title").addClass("border_red");
		}
	}
	
}

function print_product(){
	$("#print-div").jqprint();
}